package com.chanyachting.emall.action.exhibition;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.chanyachting.emall.domain.Channel;
import com.chanyachting.emall.domain.Company;
import com.chanyachting.emall.domain.Showyacht;
import com.chanyachting.emall.form.ExhibitionForm;
import com.chanyachting.emall.service.ChannelService;
import com.chanyachting.emall.service.CompanyService;
import com.chanyachting.emall.service.ShowyachtService;
import com.chanyachting.emall.serviceImpl.ChannelServiceImpl;
import com.chanyachting.emall.serviceImpl.CompanyServiceImpl;
import com.chanyachting.emall.serviceImpl.ShowyachtServiceImpl;
import com.chanyachting.emall.util.PageUtil;

public class QueryExhibitionsAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		//一般页面记录数
		int COMMON_PAGE_SIZE = 20;
		
		ExhibitionForm exForm = (ExhibitionForm) form;
		ShowyachtService syService=new ShowyachtServiceImpl();
		ChannelService cService = new ChannelServiceImpl();
//		ProductcategoryService pcService = new ProductcategoryServiceImpl();
		CompanyService cpService = new CompanyServiceImpl();
		
		int channelId = exForm.getChannelId();
		Channel c = cService.getOneChannel(new Channel(channelId));
		String channelName = exForm.getChannelName();
		if (c == null) {
			return mapping.findForward("error");
		}
		channelName = c.getName();
		
		List<Channel> subChannelList = cService.getSubChannel(c);
		
		if (subChannelList == null || subChannelList.size() == 0) {
			return mapping.findForward("error");
		}	
		
		int currentPage = exForm.getCurrentPage();
		int pageSize = COMMON_PAGE_SIZE;
		
		int recordCnt = syService.getShowyachtCount(String.valueOf(channelId),true);
		
		int pageCount = recordCnt%pageSize == 0?recordCnt/pageSize:(recordCnt/pageSize + 1);
		
		if (currentPage < 1) {
			currentPage = 1;
		} else if (currentPage > pageCount) {
			currentPage = 1;
		}
		
//		List<Productcategory> productList = pcService.getAllProductcategory();
		
		Map<String,Showyacht> showyachtList = new TreeMap<String,Showyacht>();
		List<Showyacht> tmpShowyacht = syService.getShowyachtListByPage(currentPage, pageSize,String.valueOf(channelId),true);
		
		for(int i=0;i<tmpShowyacht.size();i++) {
			Company comp = cpService.getOneCompany(new Company(tmpShowyacht.get(i).getCompanyId()));
//			showyachtList.put(getSubChannelName(productList,tmpShowyacht.get(i).getProductCategoryId()) + "`&`" + i + "`$`" + comp.getName() , tmpShowyacht.get(i));
			showyachtList.put(comp.getName() + "&" + i , tmpShowyacht.get(i));
		}
		
		exForm.setSubChannelList(subChannelList);
		exForm.setShowyachtList(showyachtList);
		exForm.setSubChannelName("");
		exForm.setChannelName(channelName);
		
		String toPageAct = PageUtil.getGotoAction(request.getRequestURL().toString(), request.getParameterMap());
		String linkStr = PageUtil.getFy3(pageCount, currentPage, toPageAct);
		String selectionStr = PageUtil.getFy4(pageCount, currentPage, toPageAct);
		exForm.setLinkStr(linkStr);
		exForm.setSelectionStr(selectionStr);
		
		return mapping.findForward("readExhibitionList");
	}
	
//	private String getSubChannelName(List<Productcategory> pList,Integer productId) {
//		String tmpStr = "";
//		for (Productcategory p:pList) {
//			if (productId.equals(p.getId())) {
//				tmpStr = p.getName();
//				break;
//			}
//		}
//		return tmpStr;
//	}
}
